package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CommonHistoryDao;
import com.moorror.codegenvel.db.sqlite.entity.CommonHistory;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.SqliteUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2025/11/2 13:21
 */
public class CommonHistoryDaoImpl extends BaseDAO<CommonHistory> implements CommonHistoryDao<CommonHistory> {

    private static CommonHistoryDaoImpl instance;

    private CommonHistoryDaoImpl() {

    }

    public static synchronized CommonHistoryDaoImpl getInstance() {
        if (null == instance) {
            instance = new CommonHistoryDaoImpl();
        }
        return instance;
    }

    String createHistoryTableSQL = "CREATE TABLE IF NOT EXISTS common_history_table (" +
            "id INTEGER PRIMARY KEY AUTOINCREMENT," +
            "kf TEXT," +
            "scene TEXT," +
            "ext_column_json TEXT," +
            "create_time INTEGER," +
            "update_time INTEGER," +
            "UNIQUE(kf, name_table, name_table_java)" +
            ")";

    /**
     * 名称不能包含$符
     * 以$作为分割
     */
    private static final String SELECT_SQL = " select id,kf,scene, ext_column_json, create_time,update_time from common_history_table ";

    @Override
    public String tableName() {
        return "common_history_table";
    }

    @Override
    public void insert(Connection conn, CommonHistory[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into common_history_table(kf, scene, ext_column_json,create_time,update_time) values(?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CommonHistory po = ary[0];
            update(conn, sql, po.getKf(), po.getScene(), po.getExtColumnJson(), po.getCreateTime(), po.getUpdateTime());
        } else {
            Object[][] args = new Object[size][5];
            for (int i = 0; i < size; i++) {
                CommonHistory po = ary[i];
                args[i][0] = po.getKf();
                args[i][1] = po.getScene();
                args[i][2] = po.getExtColumnJson();
                args[i][3] = po.getCreateTime();
                args[i][4] = po.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CommonHistory insert(Connection conn, CommonHistory po) {
        String sql = "insert into common_history_table(kf, scene, ext_column_json,create_time,update_time) values(?,?,?,?,?)";
        update(conn, sql, po.getKf(), po.getScene(), po.getExtColumnJson(), po.getCreateTime(), po.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CommonHistory updateT) {
        String sql = "delete from common_history_table where kf = ? AND scene = ? ";
        return update(conn, sql, updateT.getKf(), updateT.getScene()) > 0;
    }

    @Override
    public void update(Connection conn, CommonHistory[] ary) {

    }

    @Override
    public void update(Connection conn, CommonHistory codeGenSceneTemplate) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CommonHistory updateT) {
        String sql = "update common_history_table SET update_time = ? where kf = ? AND scene = ? ";
        return update(conn, sql, DateUtils.currentTime(), updateT.getKf(), updateT.getScene()) > 0;
    }

    @Override
    public CommonHistory findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CommonHistory findByUnique(Connection conn, CommonHistory queryT) {
        String sql = SELECT_SQL + " where kf = ? AND scene = ?  limit 1";
        List<CommonHistory> re = queryList(conn, sql, queryT.getKf(), queryT.getScene());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CommonHistory> searchLike(Connection conn, String searchValue) {
        searchValue = searchValue + "%";
        String sql = SELECT_SQL + "where scene Like ? LIMIT 20";
        List<CommonHistory> re = queryList(conn, sql, searchValue);
        if(CollectionUtils.isEmpty(re)){
            return re;
        }
        return re;
    }

    @Override
    public List<CommonHistory> queryRelate(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where kf IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public List<CommonHistory> queryIn(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where name_table IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }
}
